<?php
class MessagerAction extends UserAction{
	
	public function index(){

		$citys = M('subscriber')->Distinct(true)->field('city')->select();
		$this->assign('citys',$citys);

		$grptypes = M('subscriber')->Distinct(true)->field('grptype')->select();
		$this->assign('grptypes',$grptypes);
		
		$qrtype = M('qrcode_type')->select();
		$this->assign('sqrtype',$qrtype);
		
		C('TOKEN_ON',false);
		$this->display();
	}
	
	public function mass(){

		$qrtypeid = $this->_get('typeid'); //二维码类型
		$grptype = $this->_get('grptype'); //分组类型
		$city = $this->_get('city'); //城市
		$type = $this->_get('type'); //发送类型
		
		if(IS_POST){
			$sendtype = 1;
			$tos;
			if($qrtypeid || $city){
				if($qrtypeid){
					$whr['qrtype'] = $qrtypeid;
				}
				if($grptype){
					$whr['grptype'] = $grptype;
				}
				if($city){
					$whr['city'] = $city;
				}
				$tos = M('subscriber')->where($whr)->getField('openid',true);
			}else{
				$sendtype = 2;
				$tos = '0';
			}
			
			$content = urlencode($this->_post('response'));
			$data = WechatAdvanceAPI::PostGroupMsg($tos,$content,$type,$sendtype);
			
			$this->success ('操作成功',U('Messager/index'));
			return;
			
			/* $data = WechatAdvanceAPI::GetMediaList();
			dump($data);
			//echo $data;
			foreach ($data['item'] as $d){
				dump($d['content']);
			}
			return; */
		}
		
		$qrtype = '全部';
		if($qrtypeid){
			$where['id'] = $qrtypeid;
			$qr = M('qrcode_type')->where($where)->find();
			$qrtype = $qr['name'];
		}
		$this->assign('qr', $qrtype);
		$this->assign('city', $city == 0 ? '全部': $city);
		
		if($type==2){
			$this->display('mass_m');
		}else{
			$this->display('mass_t');
		}
		
	}
	
	public function getMaterial(){
		$data = WechatAdvanceAPI::GetMediaList();
		$items = $data['item'];
		if($items){
			$result = array();
			foreach ($items as $item){
				$media['id'] = $item['media_id'];
				$media['title'] = $item['content']['news_item'][0]['title'];
				array_push($result, $media);
			}
			$this->ajaxReturn($result,'JSON');
		}else{
			$this->ajaxReturn('0');
		}
	}
	
	public function customer(){
		
		$id = $this->_get('id');
		if(empty($id)){
			$this->error('错误的访问',U('SubscribeMember/index'));
			return;
		}
		
		$suber = M('subscriber')->where(array('id'=>$id))->find();
		if(!$suber){
			$this->error('错误的访问',U('SubscribeMember/index'));
			return;
		}
		
		if(IS_POST){
			$response = $_POST ['response'];
			
			if (strlen ( $response ) == 0) {
				$this->error ( '回复内容不能为空', U('SubscribeMember/index'));
			} else {
					
				$result = WechatAdvanceAPI::PostMsg (array('content'=>urlencode($response)), $suber['openid'], 'text' );
					
				if ($result === false) {
					$this->error ('操作失败，请检查你的AppId和AppSecret，并确保回复的请求是在48小时内的', U(SubscribeMember/index));
					return;
				} else {
					$this->success ('操作成功',U('SubscribeMember/index'));
					return;
				}
			}
		}else{
			$qr = M('qrcode_type')->where(array('id'=>$suber['qrtype']))->find();
			$this->assign('qr',$qr['name']);
			$this->assign('suber', $suber);
			$this->display();
		}
	}
}